package jim.h.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.List;
import java.util.Random;

import jim.h.commons.pojo.MaterialBO;
import jim.h.commons.pojo.SellerActivitySignBO;
import jim.h.commons.pojo.SellerActivitySignExtraBO;

import org.apache.commons.lang.time.StopWatch;
import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;

public class FastJsonTest {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        com.fasterxml.jackson.databind.ObjectMapper mapper2 = new com.fasterxml.jackson.databind.ObjectMapper();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("target/object.obj"));
        List<SellerActivitySignBO> objects = generateObjects();
        StopWatch watch = new StopWatch();

        Thread.sleep(1000L);
        watch.start();
        String jsonString = JSON.toJSONString(objects);
        watch.stop();
        System.out.println("fastjson ser:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        JSON.parseObject(jsonString, new TypeReference<List<SellerActivitySignBO>>() {
        });
        watch.stop();
        System.out.println("fastjson der:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        jsonString = mapper.writeValueAsString(objects);
        watch.stop();
        System.out.println("jackson-1 ser:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        mapper.readValue(jsonString, new org.codehaus.jackson.type.TypeReference<List<SellerActivitySignBO>>() {
        });
        watch.stop();
        System.out.println("jackson-1 der:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        jsonString = mapper2.writeValueAsString(objects);
        watch.stop();
        System.out.println("jackson-2 ser:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        mapper2.readValue(jsonString, new com.fasterxml.jackson.core.type.TypeReference<List<SellerActivitySignBO>>() {
        });
        watch.stop();
        System.out.println("jackson-2 der:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        mapper2.writeValue(new File("target/json.json"), objects);
        watch.stop();
        System.out.println("jackson-2-file ser:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        mapper2.readValue(new File("target/json.json"), new com.fasterxml.jackson.core.type.TypeReference<List<SellerActivitySignBO>>() {
        });
        watch.stop();
        System.out.println("jackson-2-file der:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        out.writeObject(objects);
        out.close();
        watch.stop();
        System.out.println("java-file ser:" + watch.getTime());

        Thread.sleep(1000L);
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("target/object.obj"));
        watch.reset();
        watch.start();
        in.readObject();
        in.close();
        watch.stop();
        System.out.println("java-file der:" + watch.getTime());

        Thread.sleep(1000L);
        watch.reset();
        watch.start();
        JSONObjectTools.list2json(objects);
        watch.stop();
        System.out.println("JSONObjectTools ser:" + watch.getTime());
    }

    private static List<SellerActivitySignBO> generateObjects() {
        Random random = new Random();
        List<SellerActivitySignBO> objects = Lists.newArrayList();
        for (int i = 0; i < 10000; i++) {
            SellerActivitySignBO o = new SellerActivitySignBO();
            o.setActivityId(random.nextLong());
            o.setApplyItemEndTime(new Date());
            o.setBrandId(random.nextLong());
            o.setBusinessId(random.nextLong());
            o.setCompScore(random.nextDouble());
            o.setId(random.nextLong());
            o.setJuCategoryId(random.nextLong());
            o.setOnlineStartTime(new Date(o.getApplyItemEndTime().getTime() + 1000L));
            o.setOnlineEndTime(new Date(o.getApplyItemEndTime().getTime() + 2000L));
            o.setShowStartTime(new Date(o.getApplyItemEndTime().getTime() + 3000L));
            o.setShowEndTime(new Date(o.getApplyItemEndTime().getTime() + 4000L));
            o.setPaid(random.nextBoolean());
            o.setPlghCode(random.nextInt() + "");
            o.setTaobaoCategoryId(random.nextLong());
            o.setSignStatus(random.nextInt());
            o.setSignTime(new Date(o.getApplyItemEndTime().getTime() + 5000L));

            MaterialBO material = new MaterialBO();
            material.setBrandBannerUrl(random.nextLong() + "");
            material.setBrandDesc(random.nextLong() + "");
            material.setBrandEnterImgUrl(random.nextLong() + "");
            material.setBrandLogoUrl(random.nextLong() + "");
            material.setEnterImgUrl(random.nextLong() + "");
            material.setLogoText(random.nextLong() + "");
            material.setNewBannerImgUrl(random.nextLong() + "");
            material.setPosterUrl(random.nextLong() + "");
            material.setVideoUrl(random.nextLong() + "");
            material.setWlBannerImgUrl(random.nextLong() + "");
            material.setWlBrandDesc(random.nextLong() + "");
            o.setMaterial(material);

            SellerActivitySignExtraBO extra = new SellerActivitySignExtraBO();
            extra.setBrandScore(random.nextInt());
            extra.setLowestDiscount(random.nextInt());
            extra.setSoldCount(random.nextInt());
            extra.setTotalPV(random.nextLong());
            extra.setTotalUV(random.nextLong());
            extra.setTurnover(random.nextLong());
            o.setActivitySignExtra(extra);

            objects.add(o);
        }
        return objects;
    }
}
